package cn.mesmile.interview.juc;

/**
 * @author zb
 * @date 2022/1/23 11:01
 * @Description
 */
public class SingleInstance {

    /**
     * volatile 保证了变量的 可见性 和 有序性
     */
    private static volatile SingleInstance singleInstance;

    private SingleInstance(){}

    public static SingleInstance getInstance() {
        if (singleInstance == null) {
            synchronized (SingleInstance.class) {
                // volatile 保证了可见性
                if (singleInstance == null) {
                    singleInstance = new SingleInstance();
                }
            }
        }
        return singleInstance;
    }
}
